import socket
import network

#连接热点
def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('meik30s', '5201314ygyu')  #配置热点名称和密码
        i = 1
        while not wlan.isconnected():
            print("正在链接...{}".format(i))
            i += 1
            time.sleep(1)
    print('network config:', wlan.ifconfig())
    
do_connect()

udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind(("", 7788))            #接受端接受数据是必须将绑定端口放在循环外面，否则会导致端口被占用

while True:
    recv_data, sender_info = udp_socket.recvfrom(1024)
    #print("{}发送{}".format(sender_info, recv_data))
    recv_data_alerts = recv_data.decode("utf-8")
    
    print(recv_data_alerts)
    #buzzer_sound(recv_data_alerts)
